package tianrun.ziguan.api.calculate.config.entity.ge;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import tianrun.ziguan.api.calculate.config.utils.DateUtil;
import tianrun.ziguan.api.calculate.config.utils.StringUtil;

import java.util.Objects;

public class TGeTableConfig {
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;
    private String tableName;
    private String columnName;
    private Type type;
    private boolean primaryKey;


    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public String getColumnName() {
        return columnName;
    }

    public void setColumnName(String columnName) {
        this.columnName = columnName;
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

    public boolean getPrimaryKey() {
        return primaryKey;
    }

    public void setPrimaryKey(boolean primaryKey) {
        this.primaryKey = primaryKey;
    }


    public enum Type {
        STRING{
            @Override
            public Object parseIn(Object value) {
                if(Objects.isNull(value))return null;
                return value.toString();
            }
            @Override
            public Object parseOut(Object value) {
                return this.parseIn(value);
            }
        }, BOOL {
            @Override
            public Object parseIn(Object value) {
                if (StringUtil.isNullOrBlank(value)) {
                    return null;
                }
                String s = String.valueOf(value);
                if ("true".equals(s) || "1".equals(s)) {
                    return 1;
                } else if ("false".equals(s) || "0".equals(s)) {
                    return 0;
                }
                return null;
            }

            @Override
            public Object parseOut(Object value) {
                return this.parseIn(value);
            }
        }, LONG {
            @Override
            public Object parseIn(Object value) {
                if (StringUtil.isNullOrBlank(value)) {
                    return null;
                }
                return Long.parseLong(value.toString());
            }

            @Override
            public Object parseOut(Object value) {
                if (StringUtil.isNullOrBlank(value)) {
                    return value;
                }
                return Long.parseLong(value.toString());
            }
        }, INT {
            @Override
            public Object parseOut(Object value) {
                if (StringUtil.isNullOrBlank(value)) {
                    return value;
                }
                return Integer.parseInt(value.toString());
            }
        }, TIME {
            @Override
            public Object parseIn(Object value) {
                if (StringUtil.isNullOrBlank(value)) {
                    return null;
                }
                String s = String.valueOf(value);
                return DateUtil.parseLocalDateTime(s);
            }

            @Override
            public Object parseOut(Object value) {
                if (StringUtil.isNullOrBlank(value)) {
                    return value;
                }
                return DateUtil.parseLong(value.toString());
            }
        };

        public Object parseIn(Object value) {
            return value;
        }

        public Object parseOut(Object value) {
            return value;
        }

    }
}
